﻿using System;
using Excel.Expressions.Compiler;
using Expressions.Compiler;
using Expressions.Compiler.Interfaces;
using Excel.Expressions.Interfaces;

namespace Excel.Expressions
{
    internal partial class Nonterminals
    {
        private static Rules _Eopt()
        {
            return new Rules
            {
                () => '+' + T[false] + Eopt[true],
                () => '-' + T[false] + Eopt[true]
            };
        }
    }

    internal partial class Expressions
    {
        private static Func<CellExpression, IExpressionTree, CellExpression> _Eopt()
        {
            return (a, b) =>
            {
                if (b[0].Context.Token.Equals('+') && b[1].Context.Token.Equals(Nonterminals.T) && (b[2] == null || b[2].Context.Token.Equals(Nonterminals.Eopt)))
                {
                    return Eopt[new Plus(a, T[a, b[1]]), b[2]];
                }
                if (b[0].Context.Token.Equals('-') && b[1].Context.Token.Equals(Nonterminals.T) && (b[2] == null || b[2].Context.Token.Equals(Nonterminals.Eopt)))
                {
                    return Eopt[new Minus(a, T[a, b[1]]), b[2]];
                }
                throw new NotSupportedException("Input token is not supported");
            };
        }
    }
}
